package com.macrohard.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macrohard.admin.domain.GradeItemExcelModel;
import com.macrohard.admin.service.GradeitemService;
import com.macrohard.admin.utils.poi.ExcelUtil;
import com.macrohard.common.base.response.R;
import com.macrohard.mapper.domain.Gradeitem;
import com.macrohard.mapper.domain.Students;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("grade-item")
@RequiredArgsConstructor
@Api(tags = "完成模块")
public class GradeItemController {
	private final GradeitemService gradeitemService;

	@GetMapping
	@ApiOperation("分页获取所有完成模块")
	public R get(@NotNull Integer current, @NotNull Integer pageSize, @NotNull Integer type) {
		Page<Gradeitem> page = new Page<>(current, pageSize);
		LambdaQueryWrapper<Gradeitem> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper = queryWrapper.eq(Gradeitem::getGitemType, type);
		page = gradeitemService.page(page, queryWrapper);
		return R.ok("ok", page);
	}

	@ApiOperation("添加完成模块")
	@PostMapping
	public R add(@RequestBody Gradeitem gradeItem) {
		return R.wrapEffectResult(gradeitemService.save(gradeItem));
	}

	@GetMapping("excel")
	public void download(HttpServletResponse response) {
		List<GradeItemExcelModel> list = gradeitemService.list().stream().map(GradeItemExcelModel::new).collect(Collectors.toList());
		ExcelUtil.exportExcel(list, "完成模块成绩", GradeItemExcelModel.class, response);
	}
}
